package com.hzzy.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzzy.bean.BaseInfo;
import com.hzzy.bean.HzClasses;
import com.hzzy.bean.HzStudent;
import com.hzzy.bean.HzTeacher;
import com.hzzy.enums.CheckinsStateEnum;
import com.hzzy.enums.UserSessionKeyEnum;
import com.hzzy.service.HzClassesService;
import com.hzzy.service.HzSemesterService;
import com.hzzy.service.HzStudentService;
import com.hzzy.utils.DateUtil;

/**
 * 考勤查看
 * @author hjb
 * 2016年5月22日
 */
@Controller
@RequestMapping("/examine")
public class ExamineController {
	private static Logger LOGGER = Logger.getLogger(ExamineController.class);
	
	@Resource
	private HzClassesService hzClassesService;
	
	@Resource
	private HzStudentService hzStudentService;
	
	@Resource
	private HzSemesterService hzSemesterService;
	
	/**
	 * 返回初始化页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/printDayPage", method=RequestMethod.POST)
	public String printDayPage(Model model, HttpServletRequest request){
		// 获取当前班主任信息
		BaseInfo baseInfo = (BaseInfo) request.getSession().getAttribute(UserSessionKeyEnum.BASE_USER_INFO.getValue());
		HzTeacher hzTeacher = baseInfo.getTeacher();
		String teacherId = hzTeacher.getTeacherId();
		// 获取当前班主任持有的班级
		List<HzClasses> classesList = hzClassesService.getInfoByTeacherId(teacherId);
		
		model.addAttribute("classesList", classesList);
		return "rightContent.examineCheckinsDay";
	}
	
	/**
	 * 返回初始化页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="printYearPage", method=RequestMethod.POST)
	public String printYearPage(Model model, HttpServletRequest request) {
		// 获取当前班主任信息
		BaseInfo baseInfo = (BaseInfo) request.getSession().getAttribute(UserSessionKeyEnum.BASE_USER_INFO.getValue());
		HzTeacher hzTeacher = baseInfo.getTeacher();
		String teacherId = hzTeacher.getTeacherId();
		// 获取当前班主任持有的班级
		List<HzClasses> classesList = hzClassesService.getInfoByTeacherId(teacherId);
		model.addAttribute("classesList", classesList);
		model.addAttribute("semesterList", hzSemesterService.getHzSemesterList(teacherId));
		return "rightContent.examineCheckinsYear";
	}
	
	/**
	 * 返回查询结果
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryByDay", method=RequestMethod.POST)
	public String queryDataByDay(Model model, HttpServletRequest request) {
		String classId = request.getParameter("classId");
		String ccdateStr = request.getParameter("ccdate"); // 点到日期
		String studentName = request.getParameter("studentName");	//学生名字
		
		model.addAttribute("classId", classId);
		model.addAttribute("ccdate", ccdateStr);
		model.addAttribute("studentName", studentName);
		if (StringUtils.isEmpty(classId)) {
			// classId不允许为空
			model.addAttribute("errMsg", "班级不能为空！");
			return printDayPage(model, request);
		}
		Date ccdate = null;
		try {
			if (StringUtils.isNotEmpty(ccdateStr)) {
				ccdate = DateUtil.toDate(ccdateStr, "yyyy-MM-dd");
			} else {
				ccdate = new Date();
				ccdateStr = DateUtil.dateFormat("yyyy-MM-dd", ccdate);
				model.addAttribute("ccdate", ccdateStr);
			}
		} catch (ParseException e) {
			model.addAttribute("errMsg", "搜索日期格式有问题，请重新选择!");
			return printDayPage(model, request);
		}
		// 获取异常点到生
		Map<Integer, List<HzStudent>> studentStateMap = new HashMap<Integer, List<HzStudent>>();
		int size = hzStudentService.getAbnormalStudents(Integer.parseInt(classId), ccdate, studentName, studentStateMap);
		if (size == 0) {
			model.addAttribute("errMsg", "没有异常点到记录^-^");
			return printDayPage(model, request);
		}
		model.addAttribute("checkinsState2", studentStateMap.get(CheckinsStateEnum.ABSENTEEISM.getValue()));
		model.addAttribute("checkinsState1", studentStateMap.get(CheckinsStateEnum.LATE.getValue()));
		model.addAttribute("checkinsState3", studentStateMap.get(CheckinsStateEnum.LEAVE.getValue()));
		return printDayPage(model, request);
	}
	
	/**
	 * 返回查询结果
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryByYear", method=RequestMethod.POST)
	public String queryDataByYear(Model model, HttpServletRequest request) {
		String classId = request.getParameter("classId");
		String semesterId = request.getParameter("semesterId"); // 点到日期
		String studentName = request.getParameter("studentName");	//学生名字
		
		model.addAttribute("classId", classId);
		model.addAttribute("semesterId", semesterId);
		model.addAttribute("studentName", studentName);
		if (StringUtils.isEmpty(classId)) {
			// classId不允许为空
			model.addAttribute("errMsg", "班级不能为空！");
			return printYearPage(model, request);
		} else if (StringUtils.isEmpty("semesterId")) {
			model.addAttribute("errMsg", "请选择学期！");
			return printYearPage(model, request);
		}
		// 获取异常点到生
		Map<Integer, List<HzStudent>> studentStateMap = new HashMap<Integer, List<HzStudent>>();
		int size = hzStudentService.getAbnormalStudents(Integer.parseInt(classId), Integer.parseInt(semesterId), studentName, studentStateMap);
		if (size == 0) {
			model.addAttribute("errMsg", "没有异常点到记录^-^");
			return printDayPage(model, request);
		}
		model.addAttribute("checkinsState2", studentStateMap.get(CheckinsStateEnum.ABSENTEEISM.getValue()));
		model.addAttribute("checkinsState1", studentStateMap.get(CheckinsStateEnum.LATE.getValue()));
		model.addAttribute("checkinsState3", studentStateMap.get(CheckinsStateEnum.LEAVE.getValue()));
		return printYearPage(model, request);
	}
	
	/**
	 * 查看异常学生记录
	 * @param model
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/checkStudent", method=RequestMethod.POST)
	public Map checkStudent(Model model, HttpServletRequest request) {
		
		String ccdate = request.getParameter("ccdate");	//查询的日期
		String sno = request.getParameter("sno");	// 学生sno
		String ccstateStr = request.getParameter("ccstate"); // 点到状态
		String classIdStr = request.getParameter("classId");	// 班级id
		
		String semesterId = request.getParameter("semesterId"); // 点到日期
		String semesterName = request.getParameter("semesterName");
		Map dataMap = new HashMap();
		List<HzStudent> list = null;
		try {
			String titleStr = "当日";
			if (StringUtils.isNotEmpty(ccdate)) {
				list = hzStudentService.findByStudentIdAndDate(sno, DateUtil.toDate(ccdate, "yyyy-MM-dd"), Integer.parseInt(ccstateStr), Integer.parseInt(classIdStr));
			} else if (StringUtils.isNotEmpty(semesterId)) {
				list = hzStudentService.findByStudentIdAndSemesterId(sno, Integer.parseInt(semesterId), Integer.parseInt(ccstateStr), Integer.parseInt(classIdStr));
				titleStr = semesterName;
			}
			dataMap.put("data", list);
			String stateName = "迟到";
			if ("2".equals(ccstateStr)) {
				stateName = "旷课";
			} else if ("3".equals(ccstateStr)) {
				stateName = "请假";
			}
			dataMap.put("header", new String[] {"序号", "班级", "学号", 
					stateName + "课程", "教师", "时间", "节次"});
			dataMap.put("resultType", "studentDetails");
			if (StringUtils.isNotEmpty(sno)) {
				dataMap.put("title", titleStr + stateName + "详情 ----" + list.get(0).getName() );
			} else {
				dataMap.put("title", titleStr + stateName + "详情 ");
			}
		} catch (Exception e) {
			LOGGER.error(e);
		}
		return dataMap;
	}
}
